

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>用户定义的采样器 (Sampler) &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="API Reference" href="../reference/index.html" />
    <link rel="prev" title="对无望的 Trial 进行剪枝 (Pruning)" href="pruning.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../installation.html">安装</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="index.html">教程</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="first.html">第一个优化例子</a></li>
<li class="toctree-l2"><a class="reference internal" href="configurations.html">高级配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="rdb.html">用 RDB 后端保存/恢复 Study</a></li>
<li class="toctree-l2"><a class="reference internal" href="distributed.html">分布式优化</a></li>
<li class="toctree-l2"><a class="reference internal" href="cli.html">命令行界面</a></li>
<li class="toctree-l2"><a class="reference internal" href="attributes.html">用户定义属性</a></li>
<li class="toctree-l2"><a class="reference internal" href="pruning.html">对无望的 Trial 进行剪枝 (Pruning)</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">用户定义的采样器 (Sampler)</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#overview-of-sampler">Sampler 概述</a></li>
<li class="toctree-l3"><a class="reference internal" href="#an-example-implementing-simulatedannealingsampler">案例: 实现模拟退火 Sampler (SimulatedAnnealingSampler)</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">教程</a> &raquo;</li>
        
      <li>用户定义的采样器 (Sampler)</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../_sources/tutorial/sampler.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="user-defined-sampler">
<span id="sampler"></span><h1>用户定义的采样器 (Sampler)<a class="headerlink" href="#user-defined-sampler" title="永久链接至标题">¶</a></h1>
<p>你可以用用户定义的 sampler 来实现：</p>
<ul class="simple">
<li><p>试验你自己的采样算法，</p></li>
<li><p>实现具体任务对应的算法来改进优化性能，或者</p></li>
<li><p>将其他的优化框架包装起来，整合进 Optuna 的流水线中 (比如  <a class="reference internal" href="../reference/integration.html#optuna.integration.SkoptSampler" title="optuna.integration.SkoptSampler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SkoptSampler</span></code></a> ).</p></li>
</ul>
<p>本节将介绍 sampler 类的内部行为，并展示一个实现用户自定义 sampler 的例子。</p>
<div class="section" id="overview-of-sampler">
<h2>Sampler 概述<a class="headerlink" href="#overview-of-sampler" title="永久链接至标题">¶</a></h2>
<p>Sampler 负责产生要被用在 trial 中求值的参数值。在目标函数内，当一个  <cite>suggest</cite> API (比如 <a class="reference internal" href="../reference/trial.html#optuna.trial.Trial.suggest_uniform" title="optuna.trial.Trial.suggest_uniform"><code class="xref py py-func docutils literal notranslate"><span class="pre">suggest_uniform()</span></code></a>) 被调用时，一个对应的分布对象 (比如 <a class="reference internal" href="../reference/distributions.html#optuna.distributions.UniformDistribution" title="optuna.distributions.UniformDistribution"><code class="xref py py-class docutils literal notranslate"><span class="pre">UniformDistribution</span></code></a>) 就会从内部被创建。Sampler 则从该分布汇总采样一个参数值。该采样值会被返回给 <cite>suggest</cite> API 的调用者，进而在目标函数内被求值。</p>
<p>为了创建一个新的 sampler, 你所定义的类需继承 <a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler" title="optuna.samplers.BaseSampler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSampler</span></code></a>.该基类提供三个抽象方法：<a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.infer_relative_search_space" title="optuna.samplers.BaseSampler.infer_relative_search_space"><code class="xref py py-meth docutils literal notranslate"><span class="pre">infer_relative_search_space()</span></code></a>, <a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.sample_relative" title="optuna.samplers.BaseSampler.sample_relative"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sample_relative()</span></code></a> 和 <a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.sample_independent" title="optuna.samplers.BaseSampler.sample_independent"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sample_independent()</span></code></a>.</p>
<p>从这些方法名可以看出，Optuna 支持两种类型的采样过程：一种是 <strong>relative sampling</strong>, 它考虑了单个 trial 内参数之间的相关性， 另一种是 <strong>independent sampling</strong>, 它对各个参数的采样是彼此独立的。</p>
<p>在一个 trial 刚开始时，<a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.infer_relative_search_space" title="optuna.samplers.BaseSampler.infer_relative_search_space"><code class="xref py py-meth docutils literal notranslate"><span class="pre">infer_relative_search_space()</span></code></a> 会被调用，它向该 trial 提供一个相对搜索空间。之后， <a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.sample_relative" title="optuna.samplers.BaseSampler.sample_relative"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sample_relative()</span></code></a> 会被触发，它从该搜索空间中对相对参数进行采样。在目标函数的执行过程中，<a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler.sample_independent" title="optuna.samplers.BaseSampler.sample_independent"><code class="xref py py-meth docutils literal notranslate"><span class="pre">sample_independent()</span></code></a> 用于对不属于该相对搜索空间的参数进行采样。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>更多细节参见 <a class="reference internal" href="../reference/samplers.html#optuna.samplers.BaseSampler" title="optuna.samplers.BaseSampler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseSampler</span></code></a> 的文档。</p>
</div>
</div>
<div class="section" id="an-example-implementing-simulatedannealingsampler">
<h2>案例: 实现模拟退火 Sampler (SimulatedAnnealingSampler)<a class="headerlink" href="#an-example-implementing-simulatedannealingsampler" title="永久链接至标题">¶</a></h2>
<p>下面的代码根据 <a class="reference external" href="https://en.wikipedia.org/wiki/Simulated_annealing">Simulated Annealing (SA)</a> 定义类一个 sampler：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">optuna</span>


<span class="k">class</span> <span class="nc">SimulatedAnnealingSampler</span><span class="p">(</span><span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">BaseSampler</span><span class="p">):</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">temperature</span><span class="o">=</span><span class="mi">100</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">RandomState</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temperature</span> <span class="o">=</span> <span class="n">temperature</span>  <span class="c1"># Current temperature.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span> <span class="o">=</span> <span class="kc">None</span>  <span class="c1"># Current state.</span>

    <span class="k">def</span> <span class="nf">sample_relative</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">search_space</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">search_space</span> <span class="o">==</span> <span class="p">{}:</span>
            <span class="k">return</span> <span class="p">{}</span>

        <span class="c1">#</span>
        <span class="c1"># An implementation of SA algorithm.</span>
        <span class="c1">#</span>

        <span class="c1"># Calculate transition probability.</span>
        <span class="n">prev_trial</span> <span class="o">=</span> <span class="n">study</span><span class="o">.</span><span class="n">trials</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">prev_trial</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
            <span class="n">probability</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">probability</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="n">prev_trial</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">_temperature</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_temperature</span> <span class="o">*=</span> <span class="mf">0.9</span>  <span class="c1"># Decrease temperature.</span>

        <span class="c1"># Transit the current state if the previous result is accepted.</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">probability</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span> <span class="o">=</span> <span class="n">prev_trial</span>

        <span class="c1"># Sample parameters from the neighborhood of the current point.</span>
        <span class="c1">#</span>
        <span class="c1"># The sampled parameters will be used during the next execution of</span>
        <span class="c1"># the objective function passed to the study.</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span> <span class="ow">in</span> <span class="n">search_space</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">param_distribution</span><span class="p">,</span> <span class="n">optuna</span><span class="o">.</span><span class="n">distributions</span><span class="o">.</span><span class="n">UniformDistribution</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s1">&#39;Only suggest_uniform() is supported&#39;</span><span class="p">)</span>

            <span class="n">current_value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_current_trial</span><span class="o">.</span><span class="n">params</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span>
            <span class="n">width</span> <span class="o">=</span> <span class="p">(</span><span class="n">param_distribution</span><span class="o">.</span><span class="n">high</span> <span class="o">-</span> <span class="n">param_distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span> <span class="o">*</span> <span class="mf">0.1</span>
            <span class="n">neighbor_low</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">current_value</span> <span class="o">-</span> <span class="n">width</span><span class="p">,</span> <span class="n">param_distribution</span><span class="o">.</span><span class="n">low</span><span class="p">)</span>
            <span class="n">neighbor_high</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">current_value</span> <span class="o">+</span> <span class="n">width</span><span class="p">,</span> <span class="n">param_distribution</span><span class="o">.</span><span class="n">high</span><span class="p">)</span>
            <span class="n">params</span><span class="p">[</span><span class="n">param_name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_rng</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="n">neighbor_low</span><span class="p">,</span> <span class="n">neighbor_high</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">params</span>

    <span class="c1">#</span>
    <span class="c1"># The rest is boilerplate code and unrelated to SA algorithm.</span>
    <span class="c1">#</span>
    <span class="k">def</span> <span class="nf">infer_relative_search_space</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">intersection_search_space</span><span class="p">(</span><span class="n">study</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">sample_independent</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">):</span>
        <span class="n">independent_sampler</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">samplers</span><span class="o">.</span><span class="n">RandomSampler</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">independent_sampler</span><span class="o">.</span><span class="n">sample_independent</span><span class="p">(</span><span class="n">study</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">param_name</span><span class="p">,</span> <span class="n">param_distribution</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>为了代码的简洁性，上面的实现没有支持一些特性 (比如 maximization). 如果你对如何实现这些特性感兴趣，请看 <a class="reference external" href="https://github.com/optuna/optuna/blob/master/examples/samplers/simulated_annealing_sampler.py">examples/samplers/simulated_annealing.py</a>.</p>
</div>
<p>你可以像使用内置的 sampler 一样使用  <code class="docutils literal notranslate"><span class="pre">SimulatedAnnealingSampler</span></code>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">objective</span><span class="p">(</span><span class="n">trial</span><span class="p">):</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">trial</span><span class="o">.</span><span class="n">suggest_uniform</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">y</span>

<span class="n">sampler</span> <span class="o">=</span> <span class="n">SimulatedAnnealingSampler</span><span class="p">()</span>
<span class="n">study</span> <span class="o">=</span> <span class="n">optuna</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span><span class="n">sampler</span><span class="o">=</span><span class="n">sampler</span><span class="p">)</span>
<span class="n">study</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span><span class="n">objective</span><span class="p">,</span> <span class="n">n_trials</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>在上面这个优化过程中，参数 <code class="docutils literal notranslate"><span class="pre">x</span></code> 和 <code class="docutils literal notranslate"><span class="pre">y</span></code> 的值都是由 <code class="docutils literal notranslate"><span class="pre">SimulatedAnnealingSampler.sample_relative</span></code> 方法采样得出的。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>严格意义上说，在第一个 trial 中，<code class="docutils literal notranslate"><span class="pre">SimulatedAnnealingSampler.sample_independent</span></code> 用于采样参数值。因为，如果没有已经完成的 trial 的话， <code class="docutils literal notranslate"><span class="pre">SimulatedAnnealingSampler.infer_relative_search_space</span></code>  中的 <a class="reference internal" href="../reference/samplers.html#optuna.samplers.intersection_search_space" title="optuna.samplers.intersection_search_space"><code class="xref py py-func docutils literal notranslate"><span class="pre">intersection_search_space()</span></code></a> 是无法对搜索空间进行推断的。</p>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../reference/index.html" class="btn btn-neutral float-right" title="API Reference" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="pruning.html" class="btn btn-neutral float-left" title="对无望的 Trial 进行剪枝 (Pruning)" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>